﻿using System.Web.Mvc;
using System;

namespace pbvs.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            decimal aa = CalculateAge(new DateTime(1935, 6, 30), new DateTime(2012, 11, 1));
            return View();
        }

        protected decimal CalculateAge(DateTime dt1, DateTime dt2)
        {
            DateTime firstDate = dt1;
            DateTime secondDate = dt2;
            if (firstDate > secondDate)
            {
                firstDate = dt2;
                secondDate = dt1;
            }

            int day1 = firstDate.DayOfYear;
            int day2 = secondDate.DayOfYear;
            decimal dateDiff = Convert.ToDecimal(Math.Abs(day1 - day2));
            decimal daysInYear = Convert.ToDecimal(new DateTime(secondDate.Year, 12, 31).DayOfYear);
            decimal yearFrac = dateDiff / daysInYear;

            return (secondDate.Year - firstDate.Year) + yearFrac;
        }
    }
}
